﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace demo10_圆角矩形
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 填充圆角矩形
        /// </summary>
        private void FillRound(Rectangle rectangle, Graphics g, Brush br, int _radius)
        {
            g.FillPath(br, DrawRoundRect(rectangle.X,
                rectangle.Y, rectangle.Width,
                rectangle.Height, _radius));
        }

        /// <summary>
        /// 圆角矩形框
        /// </summary>
        private void LineRound(Rectangle rectangle, Graphics g, Pen p, int _radius)
        {
            int temp = 0;
            if (p.Width > 1)
            {
                temp = (int)(p.Width) / 2;
            }

            g.DrawPath(p, DrawRoundRect(rectangle.X + temp,
                rectangle.Y + temp, rectangle.Width - 2 * temp,
                rectangle.Height - 2 * temp, _radius));
        }

        /// <summary>
        /// 生成圆角矩形路径
        /// </summary>
        public static GraphicsPath DrawRoundRect(int x, int y, int width, int height, int radius)
        {
            GraphicsPath gp = new GraphicsPath();
            gp.AddArc(x, y, radius, radius, 180, 90);
            gp.AddArc(x + width - radius, y, radius, radius, 270, 90);
            gp.AddArc(x + width - radius, y + height - radius, radius, radius, 0, 90);
            gp.AddArc(x, y + height - radius, radius, radius, 90, 90);
            gp.CloseAllFigures();
            return gp;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (Graphics g = this.CreateGraphics())
            {
                Rectangle rec = new Rectangle(30, 30, 400, 300);
                FillRound(rec, g, new SolidBrush(Color.Green), 100);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            using (Graphics g = this.CreateGraphics())
            {
                Rectangle rec = new Rectangle(30, 30, 400, 300);
                LineRound(rec, g, new Pen(Color.Red, 5), 100);
            }
        }
    }
}
